import { message } from 'antd';
import { getDict } from '@/services/myRecruit/talentPool';
import { getList, getInstitutions, changeInstitutions, featchApply } from '@/services/sysRules/rules';

export default {
    namespace: 'rulesList',

    state: {
        ruleType: [], // 规章制度类型
        rulesItem: [], // 规章制度列表
        rulesDetail: '',
        rulesTableList: {}
        // pagination: {
        //     pageSize: 10,
        //     current: 1,
        //     total: 0
        // }
    },
    effects: {
        *featchRuleType({ payload, callback }, { call, put }) {
            const result = yield call(getDict, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        ruleType: result.data
                    }
                });
                if (callback) callback(result.data);
            }
        },
        // 规章制度列表
        *featchList({ payload, callback }, { call }) {
            const result = yield call(getList, payload);
            if (result && result.code === 10000) {
                if (callback) callback(result.data);
            }
        },
        // 规章制度列表
        *featchDetail({ payload }, { call, put }) {
            const result = yield call(getInstitutions, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        rulesDetail: result.data
                    }
                });
            } else {
                message.error(result.msg);
            }
        },
        *feactDict(_, { call, put }) {
            const institutionLevel = yield call(getDict, {
                code: 'institutionLevel'
            });
            const institutionType = yield call(getDict, {
                code: 'institutionType'
            });
            if (institutionLevel && institutionLevel.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        institutionLevelList: institutionLevel.data
                    }
                });
            }
            if (institutionType && institutionType.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        institutionTypeList: institutionType.data
                    }
                });
            }
        },
        // 规章制度列表
        *featctInfo({ payload, callback }, { call }) {
            const result = yield call(changeInstitutions, payload);
            if (callback) callback(result);
        },
        // 发起规章制度申请
        *featchApply({ payload, callback }, { call }) {
            const result = yield call(featchApply, payload);
            if (result.code === 10000) {
                if (callback) callback(result.data);
            } else {
                message.error(result.msg);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
